package cn.com.gzheroli.core.listener;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.session.data.redis.RedisOperationsSessionRepository;

import cn.com.gzheroli.bfw.pojo.base.TSLog;
import cn.com.gzheroli.bfw.pojo.base.TSUser;
import cn.com.gzheroli.cache.SpringRedisCache;
import cn.com.gzheroli.core.common.service.CommonService;
import cn.com.gzheroli.core.constant.Globals;
import cn.com.gzheroli.core.manager.Client;
import cn.com.gzheroli.core.util.BrowserUtils;
import cn.com.gzheroli.core.util.DataUtils;
import cn.com.gzheroli.core.util.IdWorker;
import cn.com.gzheroli.core.util.LogUtil;
import cn.com.gzheroli.core.util.oConvertUtils;
public class HttpSessionMonitorListener implements HttpSessionListener{
//	@Autowired
//    private SpringRedisCache cache;
//	@Autowired
//	private CommonService commonService;
	  @Override
	    public void sessionCreated(HttpSessionEvent se) {
//	        HttpSession session = se.getSession();
//	        System.out.println("----------------------------------sessionCreated");
//	        System.out.println("sessionId:                      " + session.getId());
//
//	        System.out.println("sessionCreationTime:            " + DataUtils.formatDate(new Date(session.getCreationTime()),"yyyy-MM-dd HH:mm:ss"));
//
//	        System.out.println("sessionLastAccessedTime:        "+DataUtils.formatDate(new Date(session.getLastAccessedTime()),"yyyy-MM-dd HH:mm:ss"));
//	        
//	        int maxInterval = session.getMaxInactiveInterval();
//	        
//	        System.out.println("sessionMaxInactiveInterval(s):  " + session.getMaxInactiveInterval());
//
//	        System.out.println("sessionExpirtion:               " + DataUtils.formatDate(new Date(session.getCreationTime() + maxInterval*1000),"yyyy-MM-dd HH:mm:ss")) ;       
//
//
//	        System.out.println("----------------------------------sessionCreated");
	    }

	    @Override
	    public void sessionDestroyed(HttpSessionEvent se) {
//	        HttpSession session = se.getSession();
//	        System.out.println("----------------------------------sessionDestroyed");
//	        System.out.println("sessionId:              " + session.getId());
//	        Client client=cache.getCache(session.getId(), Client.class);
//	        if(client==null)
//	        	client=(Client)session.getAttribute(session.getId());
//	        if(null!=client){
//	        	 String broswer = client.getBroswer(); 
//	 		  	String ip=oConvertUtils.getIp();
//	 		  	Map logReqMap=new HashMap();
//	 		  	logReqMap.put("broswer", broswer);
//	 		  	logReqMap.put("ip", ip);
//	 		  	logReqMap.put("sessionId", session.getId());
//	 			TSLog log = new TSLog();
//	 			IdWorker idWorker = IdWorker.getFlowIdWorkerInstance();
//	 			log.setId(idWorker.nextIdStr());
//	 			
//	 			log.setLogContent("用户" + client.getUser().getUserName() + "已退出");
//	 			log.setLogLevel(null);
//	 			log.setOperateType(Globals.Log_Type_EXIT);
//	 			log.setNote(ip);
//	 			log.setBroswer(broswer);
//	 			log.setOperateTime(DataUtils.gettimestamp());
//	 			if(client!=null)
//	 				log.setUserId(client.getUser().getUserName());
//	 	        commonService.save(log);
//	        }
//	       
//	        cache.deleteCache(session.getId());
//	        System.out.println("sessionCreationTime:    " + DataUtils.formatDate(new Date(session.getCreationTime()),"yyyy-MM-dd HH:mm:ss"));
//
//	        System.out.println("sessionLastAccessedTime:"+DataUtils.formatDate(new Date(session.getLastAccessedTime()),"yyyy-MM-dd HH:mm:ss"));
//	        System.out.println("sessionDestroyedTime:"+DataUtils.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss"));
//
//	        System.out.println("hsName:                 "+session.getAttribute("hsName"));
//
//	        System.out.println("-----------------------------------sessionDestroyed");
	    } 
}
